package com.example.administrator.x2picturetransliteration.Home.Dialog;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.example.administrator.x2picturetransliteration.R;
import com.example.administrator.x2picturetransliteration.Util.TextViewUtil;


/**
 * 服务协议和隐私政策弹框
 * Created by Administrator on 2020/5/12.
 */

public class AgreementDialog extends Dialog {

    private AgreementHd mAgreementHd;

    public AgreementDialog(@NonNull Context context, AgreementHd mAgreementHd) {
        super(context, R.style.HomeDialog);

        this.mAgreementHd = mAgreementHd;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_dialog_agreement);

        String content1 = "请你务必审慎阅读、充分理解“服务协议”和“隐私政策”各条款，包括但不限于：为了向你提供即时通讯、内容分享等服务，我们需要收集你的设备信息、操作日志等个人信息。你可以在“设置”中查看、变更、删除个人信息并管理你的授权。";
        String content2 = "\n\n你可阅读《服务协议》和《隐私政策》了解详细信息。如你同意，请点击“同意”开始接受我们的服务。";

        SpannableString mSpannableString = new SpannableString(content1 + content2);

        TextViewUtil.setSpanColorStr(
                mSpannableString,
                content1 + content2,
                "《服务协议》",
                Color.parseColor("#5BC4E3"),
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        mAgreementHd.sbhlb("1");
                    }
                });

        TextViewUtil.setSpanColorStr(
                mSpannableString,
                content1 + content2,
                "《隐私政策》",
                Color.parseColor("#5BC4E3"),
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        mAgreementHd.sbhlb("2");
                    }
                });


        ((TextView)findViewById(R.id.ContentText)).append(mSpannableString);
        ((TextView)findViewById(R.id.ContentText)).setMovementMethod(LinkMovementMethod.getInstance());

        findViewById(R.id.NoText).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAgreementHd.onRefuse();
                AgreementDialog.this.dismiss();
            }
        });

        findViewById(R.id.OkText).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mAgreementHd.hdff();
                AgreementDialog.this.dismiss();
            }
        });
    }

    @Override
    public void show() {
        super.show();
        /**
         * 设置宽度全屏，要设置在show的后面
         */
        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        layoutParams.gravity= Gravity.CENTER;
        layoutParams.width= WindowManager.LayoutParams.MATCH_PARENT;
        layoutParams.height= WindowManager.LayoutParams.WRAP_CONTENT;

        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        getWindow().setAttributes(layoutParams);

    }

    public interface AgreementHd{
        void onRefuse();
        void hdff();
        void sbhlb(String sb);
    }
}
